<?php
/* ::: DATABASE CLASS FILE :::*/
/* This PHP class handles the data to and from the DATABSE */

class db {
    
    //Attributes
    var $connected = false; //Is true if connected
    var $resourceLink;      //Resourcelink identifier for connection
    var $conSettings;       //Array that holds the connection settings
    var $error = false;     //Is true if errors is enabled 
    
    //Constructor for DB Class
    function __construct(array $dbSettings) {
        if(is_array($dbSettings)) {
            $this->conSettings = $dbSettings;
            return true;
        } else {
            return false;
        }
    }
    
    //Method that makes a Database connection
    function connect($persistant) {
        
        //Connection to database
        if($persistant) {
            $res = mysql_pconnect($this->conSettings['server'], $this->conSettings['username'], $this->conSettings['password']);
        } else {
            $res = mysql_connect($this->conSettings['server'], $this->conSettings['username'], $this->conSettings['password']);
        }
        
        if($res) {
          if(mysql_select_db($this->conSettings['database'])) {
            $this->connected = true;
            $this->resourceLink = $res;
            return true;
          } else {
            $this->error(mysql_error());
            return false;
          }
        } else {
            $this->error(mysql_error());
            return false;
        }
    }
    
    //Disconnects the DB
    function disconnect() {
        if($this->connected == true) {
            if(mysql_close($this->resourceLink)) {
              $this->resourceLink = null;
              return true;  
            } else {
                $this->error(mysql_error($this->resourceLink));   
                return false;
            };
        }
    }
    
    //Returns MySQL data as an Array
    function get($quary) {
        if($this->connected == true) {
            if(isset($quary)){
                $result = mysql_query($quary, $this->resourceLink) or $this->error(mysql_error());
                $counter = 0;
                                
                while($data = mysql_fetch_array($result)) {
                    $return[$counter] = $data;
                    $counter++;
                }
                
                return $return;   
            } else {
                $this->error('No paremeter given in get function');
                return false;
            }
        } else {
            if($this->error) {
                $this->error('No connection made');
            }
            return false;
        }
    }
    
    //Runs a SQL command
    function run($quary) {
        if($this->connected == true) {
            if(!empty($quary)) {
                mysql_query($quary) or $this->error(mysql_error(). "<br/>SQL line: ". $quary);
                return true;
            } else {
                return false;
            }
        }
    }
    
    //Insert data into a table
    function insert($table, array $input) {
        if(!empty($table) && is_array($input)) {
            $counter = 0;
            $items = count($input);
            
            foreach($input as $data) {
                if($counter >= 1 && $counter < $items) {
                    $insertData .= ",";
                }
                $insertData .= " '$data'";
                $counter++;
            }
            $table = $this->getTable($table);
            $SQL = "INSERT INTO $table VALUES ($insertData)";
            if($this->run($SQL)) {
                return true;
            } else {
                return false;
            }
        
        } else {
            $this->error("No table or input has been given in the DB->Insert function");
            return false;
        }
    }
    
    //Updates data in row
    function update($table, array $input, $finder) {
        if(!empty($table) && is_array($input) && !empty($finder)) {
            $counter = 0;
            $items = count($input);
            $table = $this->getTable($table);
            
            foreach($input as $colum => $data) {
                if($counter >= 1 && $counter < $items) {
                    $updateData .= ", ";
                }
                    $updateData .= "$colum='$data'";
                    $counter++;
            }
            
            $SQL = "UPDATE $table SET $updateData WHERE $finder";
            if($this->run($SQL)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    //Returns the table prefix
    function getPrefix() {
        if(!empty($this->conSettings['prefix'])) {
            return $this->conSettings['prefix'];
        } else {
            return false;
        }
    }
    
        
    //Returns the tablename with prefix
    function getTable($table) {
        $prefix = $this->getPrefix();
        
        if(!empty($table)) {
            $table = $prefix. $table;
            return $table;
        } else {
            return false;
        }
    }
    
    
    //Shows error if that's enabled
    function error($err) {
        if(isset($err)) {
            if($this->error) {
                die('Error: '. $err);   
            }
        } else {
            return false;
        }
    }
    
    //Enable errors
    function enableErrors() {
        $this->error = true;
        return true;
    }
}